var express = require('express');
var router = express.Router();
let db = require('../tools/db');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.json({code:200,data:null,mag:"欢迎来到小U课堂API v1.0!"})
});
//轮播图接口
router.get("/banner",async (req,res)=>{
  let sql = `select id,concat('/uploads/',image_src) as image_src,title,isshow from e_banners where isshow=1`;
  let a = await db.query(sql,[]);
  res.json({code:200,data:a,msg:"轮播图接口成功"})
})
//首页课程列表
router.get("/courselist",async (req,res)=>{
  let {type=1,num=8} = req.query;
  let sql = `select id,cid,title,price,concat('/uploads/course/',image_src) as image_src,type_name,area_name,grade_name,subject_name,category_name from e_course where type=${type} limit 0,${num}`;
  let a = await db.query(sql,[parseInt(num)]);
  res.json({code:200,data:a,msg:"课程列表查询成功"})
})

//搜索接口
router.get("/searchlist",async (req,res)=>{
  let {keyword} = req.query;
  let sql = `select id,cid,title,price,concat('/uploads/course/',image_src) as image_src,type_name,area_name,grade_name,subject_name,category_name from e_course where title like '%${keyword}%'`;
  let a = await db.query(sql,[]);
  res.json({code:200,data:a,msg:"搜素结果成功"})
})

module.exports = router;
